package eventHandle;

import http.Request;
import http.Response;
import radio.EventAdapter;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Date;

/**
 * Created by zzf on 2017/4/26.
 */
public class ServiceHandle extends EventAdapter {

    @Override
    public void onRead(Request request) {
        try {

            String[] s = new String(request.getInputData()).split("\r\n");
            System.out.println("服务端接收："+new String(request.getInputData(),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void onWrite(Response response) throws IOException {
        byte[] data = response.getOutputData();
        ByteBuffer buffer = ByteBuffer.allocate(data.length);
        buffer.clear();
        buffer.put(data, 0, data.length);
        buffer.flip();

        response.getChannel().write(buffer);


    }

    public void onClosed(Request request) {
        String log = new Date().toString() + " from " + request.getAddress().toString();
        System.out.println(log);
    }
}
